package com.insider.dao.JDBC;

import com.insider.dao.PassKeeperDao;
import com.insider.domain.PassKeeper;
import com.insider.domain.row.PassKeeperRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class PassKeeperDaoImpl extends JdbcDaoSupport implements PassKeeperDao {

    @Autowired
    DataSource dataSource;

    private static final String PASS_KEEPER_SAVE_SQL = "INSERT INTO pass_keeper (pass_keeper_id, association, login, password, user_id) VALUES (?, ?, ?, ?, ?)";
    private static final String PASS_KEEPER_DELETE_SQL = "DELETE FROM pass_keeper WHERE pass_keeper_id=? AND user_id";
    private static final String PASS_KEEPER_SEARCH_SQL = "SELECT * FROM pass_keeper WHERE pass_keeper_id=? AND user_id=? AND association=?";
    private static final String PASS_KEEPER_SEARCH_ALL_SQL = "SELECT * FROM pass_keeper WHERE user_id=?";
    private static final String PASS_KEEPER_UPDATE_SQL = "UPDATE pass_keeper SET pass_keeper_id=?, association=?, login=?, password=?, user_id=?";

    @PostConstruct
    protected void initDao() {
        if (getDataSource() == null) {
            setDataSource(dataSource);
        }
    }

    @Override
    public void save(PassKeeper passKeeper) throws SQLException {
        getJdbcTemplate().update(PASS_KEEPER_SAVE_SQL,
                passKeeper.getId(),
                passKeeper.getAssociation(),
                passKeeper.getLogin(),
                passKeeper.getPassword(),
                passKeeper.getUserId());
    }

    @SuppressWarnings("unchecked")
    @Override
    public PassKeeper find(PassKeeper passKeeper) throws SQLException {
        return (PassKeeper) getJdbcTemplate().queryForObject(PASS_KEEPER_SEARCH_SQL,
                new Object[]{
                        passKeeper.getId(),
                        passKeeper.getAssociation(),
                        passKeeper.getAssociation()},
                new PassKeeperRowMapper());
    }

    @Override
    public void update(PassKeeper passKeeper) throws SQLException {
        getJdbcTemplate().update(PASS_KEEPER_UPDATE_SQL,
                passKeeper.getId(),
                passKeeper.getAssociation(),
                passKeeper.getLogin(),
                passKeeper.getPassword(),
                passKeeper.getUserId());
    }

    @Override
    public void delete(PassKeeper passKeeper) throws SQLException {
        getJdbcTemplate().update(PASS_KEEPER_DELETE_SQL,
                passKeeper.getId(),
                passKeeper.getUserId()
        );
    }

    @Override
    public List<PassKeeper> findAll(PassKeeper passKeeper) throws SQLException {
        List<PassKeeper> passKeepers = new ArrayList<PassKeeper>();

        List<Map<String, Object>> rows = getJdbcTemplate().queryForList(PASS_KEEPER_SEARCH_ALL_SQL,
                passKeeper.getUserId());
        for (Map row : rows) {

            PassKeeper keeper = new PassKeeper();
            keeper.setId((Long) row.get("pass_keeper_id"));
            keeper.setAssociation((String) row.get("association"));
            keeper.setLogin((String) row.get("login"));
            keeper.setPassword((String) row.get("password"));
            keeper.setUserId((Long) row.get("user_id"));
            passKeepers.add(keeper);
        }

        return passKeepers;
    }
}
